package com.vipheyue.lanjingapp.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.vipheyue.lanjingapp.R;
import com.vipheyue.lanjingapp.utils.DoubleClickExitDetector;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebActivity extends Activity {
    View view;
    @Bind(R.id.webView)
    WebView webView;
    @Bind(R.id.myProgressBar)
    ProgressBar myProgressBar;
    @Bind(R.id.iv_back)
    ImageView iv_back;
    @Bind(R.id.iv_share)
    ImageView iv_share;
    private String netVersion;
    String url = "http://www.ssq168.com/sub/lanjing/?pro=8B638128645040A1";
//    String url = "http://ssq168.com/sub/zhangppxx/?ext=FA0A34B46";
    //TODO  注意微信上面有包名和MD5
    /**************分享****************/
    UMSocialService mController;
    private UMImage mShareImageview;
    private String   shareTitle = "蓝鲸养护最棒的养护APP";
    private String shareContent="下载蓝鲸养护请到www.lanjing.com";
    //设置点击分享内容的跳转链接
    private String  targetUrl = "http://www.bbbbbb.com";
    DoubleClickExitDetector exitDetector = new DoubleClickExitDetector(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        initData();
    }

    /*****************************************/
    private void initData() {
        webView.clearHistory();
        //支持javascript
        webView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(false);
        // 设置出现缩放工具
//        webView.getSettings().setBuiltInZoomControls(false);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(false);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setInitialScale(30);//为25%，最小缩放等级
//		webView.loadUrl("http://www.baidu.com");
        webView.loadUrl(url);
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (myProgressBar == null) {
                    return;
                }
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }

                super.onProgressChanged(view, newProgress);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                //或者使用一个文本或者图片控件把其显示出来.并吧webview隐藏起来
                view.loadUrl("file:///android_asset/error.html");
            }


        });


    }

    @OnClick({R.id.iv_back, R.id.iv_reload})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                webView.goBack();
                break;
            case R.id.iv_reload:
//                webView.reload();
                preShare();
                detailShare();
                break;
        }
    }



    /**
     * 准备分享之前
     */
    private void preShare() {
        //TODO 替换id,和 密码
        /********分享功能*************************************/
        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "1105368006", "83CseMbrAsST0Bo1");
        qqSsoHandler.addToSocialSDK();

        //参数1为当前Activity， 参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, "1105368006", "83CseMbrAsST0Bo1");
        qZoneSsoHandler.addToSocialSDK();

        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        String appID = "wx96d0c7f81448fd91";//TODO 修改appid,和密码 第二个,第三个
        String appSecret = "61faafc8cb81f66c2b0549624ed5ebad";
// 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(this, appID, appSecret);
        wxHandler.addToSocialSDK();
// 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(this, appID, appSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
        //新浪微博

    }

    private void detailShare() {
        // 首先在您的Activity中添加如下成员变量
        mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        // 设置分享内容

        mController.setShareContent(shareTitle);
        // 设置分享图片, 参数2为图片的url地址
//        mController.setShareMedia(new UMImage(this, "http://www.umeng.com/images/pic/home/social/banner.png"));
        mController.setShareMedia(mShareImageview);
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
        /**QQ---------------------------------------------------*/
        QQShareContent qqShareContent = new QQShareContent();
        //设置分享文字
        qqShareContent.setShareContent(shareContent);
        //设置分享title
        qqShareContent.setTitle(shareTitle);
        //设置分享图片
        qqShareContent.setShareImage(mShareImageview);

        qqShareContent.setTargetUrl(targetUrl);
        mController.setShareMedia(qqShareContent);
        /**QQ--------END-------------------------------------------*/


        /**QZONE--------START-------------------------------------------*/

        QZoneShareContent qzone = new QZoneShareContent();
        //设置分享文字
        qzone.setShareContent(shareContent);
        //设置点击消息的跳转URL
        qzone.setTargetUrl(targetUrl);
        //设置分享内容的标题
        qzone.setTitle(shareTitle);
        //设置分享图片
        qzone.setShareImage(mShareImageview);
        mController.setShareMedia(qzone);
        /**QZONE--------END-------------------------------------------*/
        /**weixin--------start-------------------------------------------*/
        //设置微信好友分享内容
        WeiXinShareContent weixinContent = new WeiXinShareContent();
//设置分享文字
        weixinContent.setShareContent(shareContent);
//设置title
        weixinContent.setTitle(shareTitle);
//设置分享内容跳转URL
        weixinContent.setTargetUrl(targetUrl);
//设置分享图片
        weixinContent.setShareImage(mShareImageview);
        mController.setShareMedia(weixinContent);
        /**weixin--------END-------------------------------------------*/
        /**朋友圈--------start-------------------------------------------*/
//设置微信朋友圈分享内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setShareContent(shareContent);
//设置朋友圈title
        circleMedia.setTitle(shareTitle);
        circleMedia.setShareImage(mShareImageview);
        circleMedia.setTargetUrl(targetUrl);
        mController.setShareMedia(circleMedia);
        /**朋友圈--------END-------------------------------------------*/
        /**新浪--------start-------------------------------------------*/
        //设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        /**新浪--------END-------------------------------------------*/
        /**腾讯微博--------start-------------------------------------------*/
        //设置腾讯微博SSO handler
//        mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        /**腾讯微博--------END-------------------------------------------*/


        mController.getConfig().setSsoHandler(new SinaSsoHandler());

        // 是否只有已登录用户才能打开分享选择页
        mController.openShare(this, false);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();

        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }
    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder=new     AlertDialog.Builder(this);
        builder.setMessage("你确定要退出吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                    finish();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
//        if (exitDetector.onClick()) {
//            super.onBackPressed();
//        }
    }
}
